<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 部门管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.name" placeholder="名称" class="handle-input filter-item mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" class="filter-item" @click="handleSearch">搜索</el-button>
                <el-button type="primary" icon="el-icon-plus" class="filter-item" @click="handleAdd">新增</el-button>
            </div>
            <el-table
                    :data="tableData"
                    style="width: 100%;margin-bottom: 20px;"
                    row-key="id"
                    border
                    default-expand-all
                    :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                <el-table-column prop="name" label="名称" sortable width="180"></el-table-column>
                <el-table-column prop="orderNum" label="排序"></el-table-column>
                <el-table-column prop="createTime" label="创建时间" width="180">
                    <template slot-scope="scope">
                        <span>{{ parseDateTime(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                                type="text"
                                icon="el-icon-plus"
                                @click="handleDeptAdd(scope.$index, scope.row)"
                        >新增</el-button>
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                                type="text"
                                icon="el-icon-delete"
                                class="red"
                                @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="50%">
            <el-form ref="form" :model="form" :rules="rules" label-width="70px">
                <el-form-item label="上级部门" v-show="form.deptShowBoolean">
                    <select-tree v-model="form.parentId" :options="tableData" :props="defaultProps" />
                </el-form-item>
                <el-form-item label="上级部门" disabled="false" v-show="!form.deptShowBoolean">
                    <el-input v-model="form.parentId" v-show="false"></el-input>
                    <el-input v-model="form.parentName"></el-input>
                </el-form-item>
                <el-form-item label="名称" prop="name">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="orderNum">
                    <el-input v-model.number="form.orderNum"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import { sysDeptPage,delSysDept,saveSysDept,updateSysDept } from '../../api/sysDept';

    import SelectTree from '../../components/common/SelectTree.vue';
    export default {
        name: 'sys-dept-list',
        data() {
            return {
                // 遮罩层
                loading: true,
                query: {
                    name: '',
                    current: 1,
                    size: 10
                },
                tableData: [],
                delList: [],
                editVisible: false,
                form: {
                    id:0,
                    parentId:0,
                    name:"",
                    orderNum:0,
                    deptShowBoolean:true
                },
                idx: -1,
                id: -1,
                defaultProps: {
                    parent: 'parentId',   // 父级唯一标识
                    value: 'id',          // 唯一标识
                    label: 'name',       // 标签显示
                    children: 'children', // 子级
                },
                rules: {
                    name: [
                        { required: true, message: '请输入名称', trigger: 'blur' },
                        { min: 2, max: 30, message: '长度在 2-30 个字符', trigger: 'blur' }
                    ],
                    orderNum: [
                        { required: true, message: '请输入排序', trigger: 'blur' },
                        { type: 'number', message: '排序必须为数字值'}
                    ]
                }
            };
        },
        components: {
            SelectTree
        },
        created() {
            this.defaultform = JSON.parse(JSON.stringify(this.form));
            this.getData();
        },
        destroyed() {
            this.defaultform = null;
        },
        methods: {
            // 获取 easy-mock 的模拟数据
            getData() {
                this.loading = true;
                sysDeptPage(this.query).then(res => {
                    this.loading = false;
                    this.tableData = res.sysDeptList;
                    this.pageTotal = res.count;
                });
            },
            // 触发搜索按钮
            handleSearch() {
                this.$set(this.query, 'current', 1);
                this.getData();
            },
            // 删除操作
            handleDelete(index, row) {
                // 二次确认删除
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                })
                    .then(() => {
                        delSysDept(row.id).then(res => {
                            if (res.code == 0) {
                                this.editVisible = false;
                                this.$message.success(`删除成功`);
                                this.getData();
                            } else {
                                this.$message.error(res.msg);
                                return false;
                            }
                        });
                    })
                    .catch(() => {});
            },
            handleAdd(){
                this.form = Object.assign({}, this.defaultform);
                this.editVisible = true;
                this.form.deptShowBoolean = true;
                this.$nextTick(()=>{
                    this.$refs['form'].clearValidate();
                })
            },
            handleDeptAdd(index, row){
                this.idx = index;
                this.form = Object.assign({}, this.defaultform);
                this.form.parentId = row.id;
                this.form.parentName = row.name;
                this.form.deptShowBoolean = false;
                this.editVisible = true;
                this.$nextTick(()=>{
                    this.$refs['form'].clearValidate();
                })
            },
            // 编辑操作
            handleEdit(index, row) {
                this.idx = index;
                this.form.id = row.id;
                this.form.name = row.name;
                this.form.orderNum = row.orderNum;
                this.form.parentId = row.parentId;
                this.form.parentName = row.parentName;
                this.form.deptShowBoolean = false;
                this.editVisible = true;
                this.$nextTick(()=>{
                    this.$refs['form'].clearValidate();
                })
            },
            // 保存编辑
            saveEdit() {
                this.$refs['form'].validate((valid) => {
                    if (!valid) {
                        console.log('error submit!!');
                        return false;
                    }
                    if (this.form.id) {
                        updateSysDept(this.form).then(res => {
                            if (res.code == 0) {
                                this.editVisible = false;
                                this.$message.success(`修改成功`);
                                this.getData();
                            } else {
                                this.$message.error(res.msg);
                                return false;
                            }
                        });
                    } else {
                        saveSysDept(this.form).then(res => {
                            if (res.code == 0) {
                                this.editVisible = false;
                                this.$message.success(`保存成功`);
                                this.getData();
                            } else {
                                this.$message.error(res.msg);
                                return false;
                            }
                        });
                    }
                });
            }
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .red {
        color: #ff0000;
    }
    .mr10 {
        margin-right: 10px;
    }
    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>
